using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using RollManager.Library;
using System.Drawing;
using Pegasus.Windows.Forms;

namespace RollManager.Forms.ActionPanels
{
    [StructLayout( LayoutKind.Sequential )]
    public struct FLASHWINFO
    {
        public UInt32 cbSize;
        public IntPtr hwnd;
        public Int32 dwFlags;
        public UInt32 uCount;
        public Int32 dwTimeout;
    }

    public enum FLASHWINFOFLAGS
    {
        FLASHW_STOP = 0,
        FLASHW_CAPTION = 0x00000001,
        FLASHW_TRAY = 0x00000002,
        FLASHW_ALL = ( FLASHW_CAPTION | FLASHW_TRAY ),
        FLASHW_TIMER = 0x00000004,
        FLASHW_TIMERNOFG = 0x0000000C
    }

    public partial class ActionDialog : FormEx
    {
        [DllImport( "user32.dll" )]
        public static extern int FlashWindowEx( ref FLASHWINFO pfwi );

        private static ActionDialog s_singleton = new ActionDialog();

        private ActionDialog() : base( Common.GetConfigName( "actionDialog" ) )
        {
            InitializeComponent();
        }

        public static void SaveWindowPosition()
        {
            s_singleton.OnClosed( null );
        }

        public static void CloseTab( TabPage tab )
        {
            s_singleton.actionTabs.TabPages.Remove( tab );

            // Hide control if no tabs are running.
            if( s_singleton.actionTabs.TabPages.Count < 1 )
            {
                s_singleton.Hide();
            }
        }

        public static void FlashWindow()
        {
            // Make the window flash if not focused.
            FLASHWINFO fw = new FLASHWINFO();
            fw.cbSize = Convert.ToUInt32( Marshal.SizeOf( typeof( FLASHWINFO ) ) );
            fw.hwnd = s_singleton.Handle;
            fw.dwFlags = (Int32) ( FLASHWINFOFLAGS.FLASHW_ALL | FLASHWINFOFLAGS.FLASHW_TIMERNOFG );
            fw.dwTimeout = 0;

            FlashWindowEx( ref fw );
        }

        public static void StartAction( DevLevel devLevel, RollManager.Library.Action action, SubAction subAction, bool simulation )
        {
            // Create action control
            ActionControl control = new ActionControl( devLevel, action, subAction, simulation );
            control.Dock = DockStyle.Fill;

            // Create tab page to put control in.
            TabPage newTab = new TabPage( control.Name );
            newTab.Controls.Add( control );

            // Add tab page to dialog to show it.
            s_singleton.actionTabs.TabPages.Add( newTab );
            s_singleton.actionTabs.SelectedTab = newTab;

            if( !s_singleton.Visible )
            {
                s_singleton.Show();
            }
        }

        public static void FocusTab( TabPage tab )
        {
            s_singleton.actionTabs.SelectedTab = tab;
        }

        private void ActionDialog_FormClosing( object sender, FormClosingEventArgs e )
        {
            actionTabs.TabPages.Clear();
            Hide();

            e.Cancel = true;
        }
    }
}